<?php
/**
 * Created by PhpStorm.
 * User: zh
 * Date: 17/9/13
 * Time: 下午17:03
 */
class SXPPushRecord extends Eloquent
{
    protected $table = "suixingpay_push_record";

    public static function saveData($arr){
        $sxp = SXPPushRecord::where('tranSeqNo',$arr['tranSeqNo'])->first();
        if(!$sxp){
            $sxp = new SXPPushRecord();
        }
        foreach($arr as $key=>$val){
            if($val) {
                $sxp->$key = $val;
            }
        }
        $sxp->created_at = Common::currentTimeString();
        try {
            $sxp->save();
        }catch(Exception $e){
            Log::error(['随行付数据保存失败,suixingpay_error'=>$e]);
            return 'fail';
        }
        $sxp = SXPPushRecord::find($sxp->id);
        $fs = FinanceStatement::where('sxp_id', $sxp->id)->first();
        if(!$fs){
            $fs = new FinanceStatement();
            $fs->created_at = Common::currentTimeString();
        }
        $bt = DB::table('business_tunnels')->where('code',$sxp->mno)->select('shop_id')->first();
        $fs->sxp_id = $sxp->id;
        $fs->tranSts = $sxp->tranSts;
        $fs->tranTm = $sxp->tranTm;
        $fs->authCode = $sxp->authCode;
        $fs->batNo = $sxp->batNo;
        $fs->sign = $sxp->sign;
        $fs->trmNo = $sxp->trmNo;
        $fs->paySource = $sxp->paySource;
        $fs->cardNo = $sxp->cardNo;
        $fs->feeAmt = $sxp->feeAmt;
        $fs->tranSeqNo = $sxp->tranSeqNo;
        $fs->mno = $sxp->mno;
        if($bt){
            $fs->shop_id = $bt->shop_id;
            $shop = MaintenanceShops::find($bt->shop_id);
            if($shop){
                $fs->shop_name = $shop->name;
            }
        }
        $fs->tranType = $sxp->tranType;
        $fs->ordNo = $sxp->ordNo;
        $fs->tranAmt = $sxp->tranAmt;
        $fs->ouuid = $sxp->ouuid;
        $fs->posSeqNo = $sxp->posSeqNo;
        $fs->tranDt = $sxp->tranDt;
        $fs->searchNo = $sxp->searchNo;
        $fs->anhui_income_amt = intval($sxp->tranAmt * 100) - intval($sxp->feeAmt * 100);
        if(!$fs->wpos_id) {
            $fs->status = 2;
            $fs->exception_key = '01';
        }
        try {
            $fs->save();
        }catch(Exception $e){
            Log::error(json_encode($e->getMessage()));
            return 'fail';
        }
        return 'ok';
    }
}